#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//结构体
struct treenode {
	char data;
	treenode* left;
	treenode* right;
};
//栈的创建
#define maxstack 505
treenode*stack[maxstack];
int top = -1;
int isempty() {
	if (top == -1) return 1;
	else return 0;
}
int push(treenode* data) {
	stack[++top] = data;
	return 1;
}
treenode* pop() {
	return stack[top--];
}

//创建树
struct treenode* root;
treenode* build() {
	char value[510];
	scanf("%s", &value);
	int a = 0;
	while (value[a] != '\0') {
		if (value[a] > 64) {
			root = (treenode*)malloc(sizeof(struct treenode));
			root->data = value[a];
			root->left = NULL;
			root->right = NULL;
			push(root);

		}
		else {
			root = (treenode*)malloc(sizeof(struct treenode));
			root->data = value[a];
			root->right = pop();
			root->left = pop();
			push(root);
		}
	}
	root = stack[0];
	return 0;
}
//创建队列
char queue[maxstack];
int front = -1;
int rear = -1;
void enqueue(char item) {
	rear++;
	queue[rear] = item;

}
//树入队
void leveorder(treenode*root) {
	queue[++rear] = root->data;
	if (root == NULL) return;
	leveorder(root->left);
	leveorder(root->left);
}

//打印最右子树
void printqueue(char*queue) {
	front = 0;
	while (front == rear) {
		printf("%s", queue[front]);
		front = front + 2;
	}
	printf("\n");
}

int main() {
	int n;
	scanf("%d", n);
	int m;
	for(m = 0; m < n; m++) {
		build();
		rear = front = 1;
		leveorder(root);
		printqueue(queue);
	}
	return 0;
}

